<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Datei: upload.class.php
 *
 * Upload single File with given Extensions, Bytes... and put
 * it in a directory. Basic settings are set automatically by initializing
 * the class and call the upload-function.
 * There can be overridden with your own parameters.
 *
 * @author Norbert Gebert
 * @copyright Norbert Gebert 25.09.2011
 * @license GPL3
 *
 * @version 0.5b
 * @package Admin
 * @subpackage global
 */
class upload
{

	// @var string $uploadUnicName Unic-Filename TRUE or FALSE
	private $uploadUnicName = FALSE;
	// @var int $uploadErrNo Number for Errorcode
	private $uploadErrNo = 0;
	// @var string $uploadError Text for Errorcode
	private $uploadError;
	// @var string $error_1 - $error_7 Errorcodes
	private $error_1;
	private $error_2;
	private $error_3;
	private $error_4;
	private $error_5;
	private $error_6;
	private $error_7;

	/**
	 * Class-constructor
	 *
	 * @access	public
	 */
	public function __construct()
	{
		$this->error_1 = _('Nothing To Upload. Input: ');
		$this->error_2 = _('Wrong File Extension. Input: ');
		$this->error_3 = _('Too Big File Size. Input: ');
		$this->error_4 = _('Too Big File Size. Check php.ini.');
		$this->error_5 = _('Too Big File Size. Check Form.');
		$this->error_6 = _('Upload delete. Error.');
		$this->error_7 = _('No File To Upload.');
	}

	/**
	 * Start File-Upload
	 *
	 * @access	public
	 * @param	string	$uploadFilename	File name for the file
	 * @param	string	$uploadDir			Path where the file stored
	 * @param	string	$uploadMaxSize		Max. filesize in bytes
	 * @param	string	$uploadId			id/name of field in fieldset
	 * @param	string	$uploadFileExt		(optional) Allowed file extensions
	 * @param	string	$uploadChmod		(optional) File permissions
	 */
	public function doUpload($uploadFilename, $uploadDir, $uploadMaxSize, $uploadId, $uploadFileExt = array('gif', 'jpg', 'png', 'bmp'), $uploadChmod = 0644)
	{
		set_time_limit(0);
		$ok = TRUE;

		$file_ext = self::_get_file_ext($uploadFilename);

		if (!in_array($file_ext, $uploadFileExt))
		{
			$this->uploadErrNo = 5;
			$this->uploadError .= $this->error_2 . $uploadFilename;
			$ok = FALSE;
		}
		else
		{
			if (intval($_FILES[$uploadId]['size']) > $uploadMaxSize)
			{
				$this->uploadErrNo = 1;
				$this->uploadError .= $this->error_3 . $uploadFilename;
				$ok = FALSE;
			}
			else
			{
				$safe_name = preg_replace("/[^\w\.]/", "_", $uploadFilename);

				if ($this->uploadUnicName)
				{
					$safe_name = date('mdy') . '_' . mt_rand(100, 999) . '.' . $file_ext;
				}

				$uploadFilename .= $safe_name;

				if (move_uploaded_file($_FILES[$uploadId]['tmp_name'],
						$_SERVER["DOCUMENT_ROOT"] . '/images/' .
						$uploadDir . '/' . $safe_name) == FALSE)
				{
					$this->uploadErrNo = $_FILES[$uploadId]['error'];

					switch ($this->uploadErrNo)
					{
						case 1 :
							$this->uploadError .= $this->error_4;
							$ok = FALSE;
						case 2 :
							$this->uploadError .= $this->error_5;
							$ok = FALSE;
						case 3 :
							$this->uploadError .= $this->error_6;
							$ok = FALSE;
						case 4 :
							$this->uploadError .= $this->error_7;
							$ok = FALSE;
						default :
							@chmod($uploadDir . $safe_name, $uploadChmod);
					}
				}
			}
		}
		return $ok;
	}

	/**
	 * Extract file extension
	 *
	 * @access	private
	 * @param	string	$file		Filename
	 * @return	string				Fileextension
	 */
	private function _get_file_ext($file)
	{
		return strtolower(str_replace(".", "", substr($file, strrpos($file, '.'))));
	}

	/**
	 * Returns Errortext
	 * @return string
	 */
	public function getError()
	{
		return $this->uploadError;
	}

}

?>